package com.javasvup.servicecode.controller;


import com.javasvip.common.pojo.AuthCode;
import com.javasvup.servicecode.dao.AuthCodeDao;
import com.javasvup.servicecode.service.EmailTService;
import com.javasvup.servicecode.service.TolenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/code")
public class TokenController {
    @Autowired
    TolenService tolenService;
    @Autowired
    AuthCodeDao authCodeDao;

    @Autowired
    EmailTService emailTService;




    /**
     * 根据邮件下发验证码
     * @param email
     * @return
     */
    @GetMapping("/create/{email}")
    public String VerificationCode(@PathVariable String email) {
        System.out.println("code========");
        int newNum = (int)((Math.random()*9+1)*100000);//随机码
        //保存到数据库
        AuthCode authCode=new AuthCode();
        authCode.setEmail(email);//邮件
        authCode.setCode(newNum+"");//验证码

        Date date=new Date();
        Timestamp createTime=new Timestamp(date.getTime());
        Timestamp expiretime=new Timestamp(date.getTime()+(1000*60*10));
        authCode.setCreatetime(createTime);
        authCode.setExpiretime(expiretime);
        authCodeDao.save(authCode);

        emailTService.sendMail(email,newNum+"");
        System.out.println("code===结束=====");
        return "成功";
    }


    /**
     * 判断验证码是否正确
     * @param email
     * @return
     */
    @GetMapping("/verification/{email}/{code}")
    public Integer verification(@PathVariable String email,@PathVariable String code) {
        System.out.println( "===验证："+ email+":"+code);
        List<AuthCode> authCodes = authCodeDao.queryByEmail(email);
        if (authCodes!=null && authCodes.size()>0){
            AuthCode authCode=authCodes.get(0);
            System.out.println( "==="+ authCode);
            SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date time=new Date();
                Date endTime=sdf1.parse("2020-11-01 16:30:48.0");
                if(time.getTime()>endTime.getTime()){
                    if(code.equals(authCode.getCode())){
                        System.out.println("验证成功"+code+":"+authCode.getCode());

                        return 0;
                    }else{
                        System.out.println( "验证失败");
                        return 1;
                    }

                }
            }catch (Exception e){
                e.printStackTrace();
            }

            return 1;
        }else  {
            return 1;
        }
    }

    public static void main(String[] args)    {
        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            sdf1.parse("2020-11-01 16:30:48.0");
            System.out.println( sdf1.parse("2020-11-01 16:30:48"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }



}
